/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package org.sbbs.admin.entity;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Index;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import jakarta.persistence.Transient;
import jakarta.validation.constraints.NotNull;
import java.util.HashSet;
import java.util.Set;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.hibernate.annotations.Comment;
import org.hibernate.annotations.Formula;
import org.sbbs.base.jpa.entity.BaseEntity;
import org.sbbs.base.jpa.entity.BaseTreeEntity;

/**
 *
 * @author jenkins
 */
@Data
@EqualsAndHashCode(callSuper = true)
@Entity
@Table(name = "sys_area",
        indexes = {
            @Index(name = "idx_pid_fk", columnList = "pid")}
)

@Comment("系统区域表")
public class SysArea extends BaseTreeEntity {

    private static final long serialVersionUID = -4488285120150231154L;
//        @Id
//    @Column(name = "id", nullable = false)
//    @Comment("主键ID")
////    @GeneratedValue(strategy = GenerationType.IDENTITY)
//    private Long id;
    
//    @Comment("名称")
//    @Column(name = "name", length = 50)
//    @NotNull
//    private String name;
//    @Comment("父级区域代码1")
//    @ManyToOne
//    @JoinColumn(name = "pid", nullable = true)
////     @ForeignKey(name="FK_PARENT_ID")
//    @Transient
//    private SysArea parent;
//    
//    @Comment("父级区域代码")
//    @Column(name = "pid")
//    private Long pid;

//    @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
//    private Set<SysArea> children=new HashSet<SysArea>();
//    @Comment("级别: 1-省/直辖市, 2-市, 3-区/县/地级市, 4-街道/镇, 5-社区/乡村")
//    @Column(name = "level")
//    @NotNull
//    private Integer level;

    @Comment("省/直辖市代码")
    @Column(name = "province_code")
    @NotNull
    private Long provinceCode;
    @Comment("区市代码")
    @Column(name = "city_code")
    @NotNull
    private Long cityCode;
    @Comment("区县代码")
    @Column(name = "area_code")
    @NotNull
    private Long areaCode;
    @Comment("街道/镇代码")
    @Column(name = "street_code")
    @NotNull
    private Long streetCode;
    @Comment("社区/乡村代码")
    @Column(name = "committee_code")
    @NotNull
    private Long committeeCode;
    @Comment("城乡分类代码")
    @Column(name = "committee_type")
    @NotNull
    private Integer committeeType;

//    @Transient
    @Formula("(select count(1)>0 from sys_area t where t.pid=id)")
    private boolean hasChildren;
//    @Transient
    @Formula("level>4")
    private boolean leaf;

//    @Transient
//    public boolean isHasChildren() {
//        return !this.level.equals(5);
//    }
}
